﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessEntities.Model;
namespace BusinessEntities
{
    public static class TransferHelper
    {
        private static Dictionary<int, City> CityCache = new Dictionary<int, City>();
        private static Dictionary<int, User> UserCache = new Dictionary<int, User>();
        private static Dictionary<int, Event> EventCache = new Dictionary<int, Event>();

        public static Guest TransferGuest(Guest guest)
        {
            if (guest == null)
            {
                return null;
            }
            Guest retGuest = new Guest();
            retGuest.ApartmentNumber = guest.ApartmentNumber;
            retGuest.City = TransferCity(guest.City);
            retGuest.CityID = guest.CityID;
            retGuest.Couple = guest.Couple;
            retGuest.Email = guest.Email;
            retGuest.FemaleFirstName = guest.FemaleFirstName;
            retGuest.HouseNumber = guest.HouseNumber;
            retGuest.ID = guest.ID;
            retGuest.LastName = guest.LastName;
            retGuest.MaleFirstName = guest.MaleFirstName;
            retGuest.Street = guest.Street;
            retGuest.User = TransferUser(guest.User);
            retGuest.UserID = guest.UserID;           
            return retGuest;
        }

        public static User TransferUser(User user)
        {
            if (user == null)
            {
                return null;
            }
            if (UserCache.ContainsKey(user.ID))
            {
                return UserCache[user.ID];
            }
            User retUser = new User();
            retUser.Activated = user.Activated;
            retUser.Email = user.Email;
            retUser.FirstName = user.FirstName;
            retUser.ID = user.ID;
            retUser.LastName = user.LastName;
            retUser.Password = user.Password;
            retUser.Role = user.Role;
            UserCache[user.ID] = retUser;
            return retUser;
        }

        public static City TransferCity(City city)
        {
            if (city == null)
            {
                return null;
            }
            if (!CityCache.ContainsKey(city.ID))
            {
                CityCache[city.ID] = new City();
                CityCache[city.ID].ID = city.ID;
                CityCache[city.ID].Name = city.Name;
            }
            return CityCache[city.ID];
        }

        public static Event TransferEvent(Event _event)
        {
            if (_event == null)
            {
                return null;
            }
            if (EventCache.ContainsKey(_event.ID))
            {
                return EventCache[_event.ID];
            }
            var retEvent = new Event();
            retEvent.DateOfEvent = _event.DateOfEvent;
            retEvent.EventType = _event.EventType;
            retEvent.FemaleActor = _event.FemaleActor;
            retEvent.Hall = TransferHall(_event.Hall);
            retEvent.HallID = _event.HallID;
            retEvent.ID = _event.ID;
            retEvent.MaleActor = _event.MaleActor;
            retEvent.Name = _event.Name;
            retEvent.User = TransferUser(_event.User);
            retEvent.UserID = _event.UserID;
            EventCache[_event.ID] = retEvent;
            return EventCache[_event.ID];
        }

        public static Hall TransferHall(Hall hall)
        {
            if (hall == null)
            {
                return null;                
            }
            var retHall = new Hall();
            retHall.City = TransferCity(hall.City);
            retHall.CityID = hall.CityID;
            retHall.ID = hall.ID;
            retHall.MaxAllowed = hall.MaxAllowed;
            retHall.Name = hall.Name;
            retHall.Number = hall.Number;
            retHall.Street = hall.Street;
            return retHall;
        }

        public static Invite TransferInvite(Invite invite)
        {
            if (invite == null)
            {
                return null;
            }
            var retInvite = new Invite();
            retInvite.AdditionalInvitedGuests = invite.AdditionalInvitedGuests;
            retInvite.Event = TransferEvent(invite.Event);
            retInvite.EventID = invite.EventID;
            retInvite.Gift = TransferGift(invite.Gift);
            retInvite.GiftID = invite.GiftID;
            retInvite.Guest = TransferGuest(invite.Guest);
            retInvite.GuestID = invite.GuestID;
            retInvite.Hash = invite.Hash;
            retInvite.ID = invite.ID;
            retInvite.ReservedGuests = invite.ReservedGuests;
            retInvite.RSVPd = invite.RSVPd;            
            return retInvite;
        }

        public static Gift TransferGift(Gift gift)
        {
            if (gift == null)
            {
                return null;
            }
            switch (gift.GetType().Name)
            {
                case "Check":
                    return TransferCheck((Check)gift);
                    
                case "Cash":
                    return TransferCash((Cash)gift);
                    
                case "GiftItem":
                    return TransferGiftItem((GiftItem)gift);
                    
                default:
                    break;
            }
            return null;
        }

        public static Gift TransferGiftItem(GiftItem giftItem)
        {
            GiftItem retItem = new GiftItem();
            retItem.Description = giftItem.Description;
            retItem.ID = giftItem.ID;
            retItem.Name = giftItem.Name;
            retItem.StoreBought = giftItem.StoreBought;
            return retItem;
        }

        public static Gift TransferCash(Cash cash)
        {
            Cash retCash = new Cash();
            retCash.Amount = cash.Amount;
            retCash.Currency = cash.Currency;
            retCash.Description = cash.Description;
            retCash.ID = cash.ID;
            return retCash;
        }

        public static Gift TransferCheck(Check check)
        {
            Check retCheck = new Check();
            retCheck.Amount = check.Amount;
            retCheck.Currency = check.Currency;
            retCheck.DepositDate = check.DepositDate;
            retCheck.Description = check.Description;
            retCheck.ID = check.ID;
            return retCheck;
        }
    }
}
